<?php

declare(strict_types=1);

namespace App\AdminApi\Module\Admin\Api;

use App\AdminApi\AdminApiBasicsController;
use App\AdminApi\Module\Admin\Service\JobService;
use App\AdminApi\Module\Admin\Validate\JobValidate;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\{Controller, GetMapping, PostMapping, RequestMapping};
use Psr\Http\Message\ResponseInterface;
use Zms\AdminElementPlus\FormCreate\ElemenuForm;
use Zms\Guard\Auth;
use Zms\HyperfResponse\Response;
use Zms\HyperfValidateAnnotations\Validate;

#[
    Controller(prefix: "/admin-api/admin-job", server: self::SERVER),
    Auth(guard: self::GUARD, label: ['后台管理', '职位'], app: self::APP)
]
class JobController extends AdminApiBasicsController
{
    #[Inject]
    protected JobService $service;

    #[
        GetMapping('list'),
        Auth(sign: 'admin.job.list', view: '/admin/job/index'),
    ]
    public function list(): ResponseInterface
    {
        $where = $this->request->inputs(['id', 'name', 'status']);
        $paginate = $this->service->search($where)->orderByRaw($this->orderBy('id DESC'))->paginate($this->pageSize());
        return Response::Api()->success(data: $paginate);
    }


    #[
        RequestMapping('created'),
        Auth(sign: 'admin.job.created', logger: "POST"),
        ElemenuForm(JobService::class, 'createdForm'),
        Validate(class: JobValidate::class, scene: "created")
    ]
    public function created(): ResponseInterface
    {
        $data = $this->request->getParsedBody();
        if ($this->service->created($data)) {
            return Response::Api()->success('职位添加成功');
        }
        return Response::Api()->fail('职位添加失败', $data);
    }


    #[
        RequestMapping('updated'),
        Auth(sign: 'admin.job.updated', logger: "POST"),
        ElemenuForm(JobService::class, 'updatedForm', ['id']),
        Validate(class: JobValidate::class, scene: "updated")
    ]
    public function updated(): ResponseInterface
    {
        $data = $this->request->getParsedBody();
        if ($this->service->updated($data)) {
            return Response::Api()->success('职位编辑成功');
        }
        return Response::Api()->fail('职位编辑失败', $data);
    }


    #[
        PostMapping('status'),
        Auth(relyon: ["admin.job.updated"], logger: false, describe: '修改职位状态'),
        Validate(rules: \Zms\Constants\Validate::CHANGE_STATUS_RULES)
    ]
    public function status(): ResponseInterface
    {
        $data = $this->request->inputs(['id', 'status']);
        if ($this->service->search(['id' => $data['id']])->update(['status' => $data['status']])) {
            return Response::Api()->success('更改成功');
        }
        return Response::Api()->fail('更改失败');
    }


    #[
        PostMapping('remove'),
        Auth(sign: 'admin.job.remove')
    ]
    public function remove(): ResponseInterface
    {
        $id = $this->request->input('id', '0');
        $role = $this->service->search(['id' => $id])->delete();
        if ($role) {
            return Response::Api()->success('职位已删除');
        }
        return Response::Api()->fail('职位删除失败');
    }


    #[
        GetMapping('details'),
        Auth(label: ['职位详情'], relyon: ['admin.job.list', 'admin.job.created', 'admin.job.updated'], logger: false)
    ]
    public function details(): ResponseInterface
    {
        $id = $this->request->input('id', '0');
        $role = $this->service->search(['id' => $id])->first();
        if ($role) {
            return Response::Api()->success(data: $role->toArray());
        }
        return Response::Api()->fail('职位不存在或已删除');
    }
}